﻿// =================================================================== 
// 项目说明
//====================================================================
// SunCity@Copy Right 2006-2008
// 文件： Menus.cs
// 项目名称：SunCity_MS
// 创建时间：2015/3/18
// 负责人：Chris
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using EMS20.Entity;
using EMS20.IDataAccessLayer;

namespace EMS20.DataAccessLayer
{
    /// <summary>
    /// 数据层实例化接口类 dbo.Menus.
    /// </summary>
    public partial class MenusDataAccessLayer : IMenusDataAccessLayer
    {
		#region 构造函数
		/// <summary>
		/// ??????
		/// </summary>
		public MenusDataAccessLayer()
		{
		}
		#endregion

        #region -----------实例化接口函数-----------
		
		#region 添加更新删除
		/// <summary>
		/// 向数据库中插入一条新记录。
		/// </summary>
		/// <param name="_MenusModel">Menus实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Create_MenusInsert(MenusEntity _MenusModel)
		{
			string sqlStr="CreateUpdateDelete_MenusEntity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@m_id",SqlDbType.Int),
			new SqlParameter("@m_parent_mid",SqlDbType.Int),
			new SqlParameter("@m_name",SqlDbType.VarChar),
			new SqlParameter("@m_url",SqlDbType.VarChar),
			new SqlParameter("@m_sortid",SqlDbType.Int),
			new SqlParameter("@m_status",SqlDbType.Int),
			new SqlParameter("@m_picname",SqlDbType.VarChar),
			new SqlParameter("@m_level",SqlDbType.Int),
			new SqlParameter("@m_remark",SqlDbType.VarChar),
			new SqlParameter("@m_exp1",SqlDbType.VarChar),
			new SqlParameter("@m_exp2",SqlDbType.Int),
			new SqlParameter("@m_add_time",SqlDbType.DateTime)
			};
			_param[0].Value=0;
			_param[1].Value=_MenusModel.m_id;
			_param[2].Value=_MenusModel.m_parent_mid;
			_param[3].Value=_MenusModel.m_name;
			_param[4].Value=_MenusModel.m_url;
			_param[5].Value=_MenusModel.m_sortid;
			_param[6].Value=_MenusModel.m_status;
			_param[7].Value=_MenusModel.m_picname;
			_param[8].Value=_MenusModel.m_level;
			_param[9].Value=_MenusModel.m_remark;
			_param[10].Value=_MenusModel.m_exp1;
			_param[11].Value=_MenusModel.m_exp2;
			_param[12].Value=_MenusModel.m_add_time;
			res = (int)SqlHelper.ExecuteScalar(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
			return res;
		}
		/// <summary>
		/// 向数据库中插入一条新记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_MenusModel">Menus实体</param>
		/// <returns>新插入记录的编号</returns>
		public int Create_MenusInsert(SqlTransaction sp,MenusEntity _MenusModel)
		{
			string sqlStr="CreateUpdateDelete_MenusEntity";
			int res;
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@m_id",SqlDbType.Int),
			new SqlParameter("@m_parent_mid",SqlDbType.Int),
			new SqlParameter("@m_name",SqlDbType.VarChar),
			new SqlParameter("@m_url",SqlDbType.VarChar),
			new SqlParameter("@m_sortid",SqlDbType.Int),
			new SqlParameter("@m_status",SqlDbType.Int),
			new SqlParameter("@m_picname",SqlDbType.VarChar),
			new SqlParameter("@m_level",SqlDbType.Int),
			new SqlParameter("@m_remark",SqlDbType.VarChar),
			new SqlParameter("@m_exp1",SqlDbType.VarChar),
			new SqlParameter("@m_exp2",SqlDbType.Int),
			new SqlParameter("@m_add_time",SqlDbType.DateTime)
			};
			_param[0].Value=0;
			_param[1].Value=_MenusModel.m_id;
			_param[2].Value=_MenusModel.m_parent_mid;
			_param[3].Value=_MenusModel.m_name;
			_param[4].Value=_MenusModel.m_url;
			_param[5].Value=_MenusModel.m_sortid;
			_param[6].Value=_MenusModel.m_status;
			_param[7].Value=_MenusModel.m_picname;
			_param[8].Value=_MenusModel.m_level;
			_param[9].Value=_MenusModel.m_remark;
			_param[10].Value=_MenusModel.m_exp1;
			_param[11].Value=_MenusModel.m_exp2;
			_param[12].Value=_MenusModel.m_add_time;
			res = (int)SqlHelper.ExecuteScalar(sp,CommandType.StoredProcedure,sqlStr,_param);
			return res;
		}


		/// <summary>
		/// 向数据表Menus更新一条记录。
		/// </summary>
		/// <param name="_MenusModel">_MenusModel</param>
		/// <returns>影响的行数</returns>
		public int Create_MenusUpdate(MenusEntity _MenusModel)
		{
            string sqlStr="CreateUpdateDelete_MenusEntity";
			SqlParameter[] _param={
				new SqlParameter("@DataAction",SqlDbType.Int),
				new SqlParameter("@m_id",SqlDbType.Int),
				new SqlParameter("@m_parent_mid",SqlDbType.Int),
				new SqlParameter("@m_name",SqlDbType.VarChar),
				new SqlParameter("@m_url",SqlDbType.VarChar),
				new SqlParameter("@m_sortid",SqlDbType.Int),
				new SqlParameter("@m_status",SqlDbType.Int),
				new SqlParameter("@m_picname",SqlDbType.VarChar),
				new SqlParameter("@m_level",SqlDbType.Int),
				new SqlParameter("@m_remark",SqlDbType.VarChar),
				new SqlParameter("@m_exp1",SqlDbType.VarChar),
				new SqlParameter("@m_exp2",SqlDbType.Int),
				new SqlParameter("@m_add_time",SqlDbType.DateTime)
				};
				_param[0].Value=1;
				_param[1].Value=_MenusModel.m_id;
				_param[2].Value=_MenusModel.m_parent_mid;
				_param[3].Value=_MenusModel.m_name;
				_param[4].Value=_MenusModel.m_url;
				_param[5].Value=_MenusModel.m_sortid;
				_param[6].Value=_MenusModel.m_status;
				_param[7].Value=_MenusModel.m_picname;
				_param[8].Value=_MenusModel.m_level;
				_param[9].Value=_MenusModel.m_remark;
				_param[10].Value=_MenusModel.m_exp1;
				_param[11].Value=_MenusModel.m_exp2;
				_param[12].Value=_MenusModel.m_add_time;
			return SqlHelper.ExecuteNonQuery(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
		}
		/// <summary>
		/// 向数据表Menus更新一条记录。带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
		/// <param name="_MenusModel">_MenusModel</param>
		/// <returns>影响的行数</returns>
		public int Create_MenusUpdate(SqlTransaction sp,MenusEntity _MenusModel)
		{
            string sqlStr="CreateUpdateDelete_MenusEntity";
			SqlParameter[] _param={
				new SqlParameter("@DataAction",SqlDbType.Int),
				new SqlParameter("@m_id",SqlDbType.Int),
				new SqlParameter("@m_parent_mid",SqlDbType.Int),
				new SqlParameter("@m_name",SqlDbType.VarChar),
				new SqlParameter("@m_url",SqlDbType.VarChar),
				new SqlParameter("@m_sortid",SqlDbType.Int),
				new SqlParameter("@m_status",SqlDbType.Int),
				new SqlParameter("@m_picname",SqlDbType.VarChar),
				new SqlParameter("@m_level",SqlDbType.Int),
				new SqlParameter("@m_remark",SqlDbType.VarChar),
				new SqlParameter("@m_exp1",SqlDbType.VarChar),
				new SqlParameter("@m_exp2",SqlDbType.Int),
				new SqlParameter("@m_add_time",SqlDbType.DateTime)
				};
				_param[0].Value=1;
				_param[1].Value=_MenusModel.m_id;
				_param[2].Value=_MenusModel.m_parent_mid;
				_param[3].Value=_MenusModel.m_name;
				_param[4].Value=_MenusModel.m_url;
				_param[5].Value=_MenusModel.m_sortid;
				_param[6].Value=_MenusModel.m_status;
				_param[7].Value=_MenusModel.m_picname;
				_param[8].Value=_MenusModel.m_level;
				_param[9].Value=_MenusModel.m_remark;
				_param[10].Value=_MenusModel.m_exp1;
				_param[11].Value=_MenusModel.m_exp2;
				_param[12].Value=_MenusModel.m_add_time;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.StoredProcedure,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表Menus中的一条记录
		/// </summary>
	    /// <param name="m_id">m_id</param>
		/// <returns>影响的行数</returns>
		public int Create_MenusDelete(int m_id)
		{
        MenusEntity _MenusModel =new MenusEntity();
			string sqlStr="CreateUpdateDelete_MenusEntity";
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.VarChar),
			new SqlParameter("@m_id",SqlDbType.Int),			
			new SqlParameter("@m_parent_mid",SqlDbType.Int),
			new SqlParameter("@m_name",SqlDbType.VarChar),
			new SqlParameter("@m_url",SqlDbType.VarChar),
			new SqlParameter("@m_sortid",SqlDbType.Int),
			new SqlParameter("@m_status",SqlDbType.Int),
			new SqlParameter("@m_picname",SqlDbType.VarChar),
			new SqlParameter("@m_level",SqlDbType.Int),
			new SqlParameter("@m_remark",SqlDbType.VarChar),
			new SqlParameter("@m_exp1",SqlDbType.VarChar),
			new SqlParameter("@m_exp2",SqlDbType.Int),
			new SqlParameter("@m_add_time",SqlDbType.DateTime)
			};
			_param[0].Value=2;
			_param[1].Value=m_id;
			_param[2].Value=_MenusModel.m_parent_mid;
			_param[3].Value=_MenusModel.m_name;
			_param[4].Value=_MenusModel.m_url;
			_param[5].Value=_MenusModel.m_sortid;
			_param[6].Value=_MenusModel.m_status;
			_param[7].Value=_MenusModel.m_picname;
			_param[8].Value=_MenusModel.m_level;
			_param[9].Value=_MenusModel.m_remark;
			_param[10].Value=_MenusModel.m_exp1;
			_param[11].Value=_MenusModel.m_exp2;
			_param[12].Value=_MenusModel.m_add_time;
			return SqlHelper.ExecuteNonQuery(Conn.SqlConn,CommandType.StoredProcedure,sqlStr,_param);
		}
		
		/// <summary>
		/// 删除数据表Menus中的一条记录,带事务
		/// </summary>
		/// <param name="sp">事务对象</param>
	    /// <param name="m_id">m_id</param>
		/// <returns>影响的行数</returns>
		public int Create_MenusDelete(SqlTransaction sp,int m_id)
		{
         MenusEntity _MenusModel =new MenusEntity();
			string sqlStr="CreateUpdateDelete_MenusEntity";
			SqlParameter[] _param={
			new SqlParameter("@DataAction",SqlDbType.Int),
			new SqlParameter("@m_id",SqlDbType.Int),
			new SqlParameter("@m_parent_mid",SqlDbType.Int),
			new SqlParameter("@m_name",SqlDbType.VarChar),
			new SqlParameter("@m_url",SqlDbType.VarChar),
			new SqlParameter("@m_sortid",SqlDbType.Int),
			new SqlParameter("@m_status",SqlDbType.Int),
			new SqlParameter("@m_picname",SqlDbType.VarChar),
			new SqlParameter("@m_level",SqlDbType.Int),
			new SqlParameter("@m_remark",SqlDbType.VarChar),
			new SqlParameter("@m_exp1",SqlDbType.VarChar),
			new SqlParameter("@m_exp2",SqlDbType.Int),
			new SqlParameter("@m_add_time",SqlDbType.DateTime)
			};
			_param[0].Value=2;
			_param[1].Value=m_id;
			_param[2].Value=_MenusModel.m_parent_mid;
			_param[3].Value=_MenusModel.m_name;
			_param[4].Value=_MenusModel.m_url;
			_param[5].Value=_MenusModel.m_sortid;
			_param[6].Value=_MenusModel.m_status;
			_param[7].Value=_MenusModel.m_picname;
			_param[8].Value=_MenusModel.m_level;
			_param[9].Value=_MenusModel.m_remark;
			_param[10].Value=_MenusModel.m_exp1;
			_param[11].Value=_MenusModel.m_exp2;
			_param[12].Value=_MenusModel.m_add_time;
			return SqlHelper.ExecuteNonQuery(sp,CommandType.StoredProcedure,sqlStr,_param);
		}
		#endregion
		
		#region 数据实体
		/// <summary>
		/// 得到  menus 数据实体
		/// </summary>
		/// <param name="row">row</param>
		/// <returns>menus 数据实体</returns>
		public MenusEntity Populate_MenusEntity_FromDr(DataRow row)
		{
			MenusEntity Obj = new MenusEntity();
			if(row!=null)
			{
				Obj.m_id = (( row["m_id"])==DBNull.Value)?0:Convert.ToInt32( row["m_id"]);
				Obj.m_parent_mid = (( row["m_parent_mid"])==DBNull.Value)?0:Convert.ToInt32( row["m_parent_mid"]);
				Obj.m_name =  row["m_name"].ToString();
				Obj.m_url =  row["m_url"].ToString();
				Obj.m_sortid = (( row["m_sortid"])==DBNull.Value)?0:Convert.ToInt32( row["m_sortid"]);
				Obj.m_status = (( row["m_status"])==DBNull.Value)?0:Convert.ToInt32( row["m_status"]);
				Obj.m_picname =  row["m_picname"].ToString();
				Obj.m_level = (( row["m_level"])==DBNull.Value)?0:Convert.ToInt32( row["m_level"]);
				Obj.m_remark =  row["m_remark"].ToString();
				Obj.m_exp1 =  row["m_exp1"].ToString();
				Obj.m_exp2 = (( row["m_exp2"])==DBNull.Value)?0:Convert.ToInt32( row["m_exp2"]);
				Obj.m_add_time = (( row["m_add_time"])==DBNull.Value)?Convert.ToDateTime(1900-1-1):Convert.ToDateTime( row["m_add_time"]);
			}
			else
			{
				return null;
			}
			return Obj;
		}
		
        /// <summary>
		/// 得到  menus 数据实体
		/// </summary>
		/// <param name="dr">dr</param>
		/// <returns>menus 数据实体</returns>
		public MenusEntity Populate_MenusEntity_FromDr(IDataReader dr)
		{
			MenusEntity Obj = new MenusEntity();
			
				Obj.m_id = (( dr["m_id"])==DBNull.Value)?0:Convert.ToInt32( dr["m_id"]);
				Obj.m_parent_mid = (( dr["m_parent_mid"])==DBNull.Value)?0:Convert.ToInt32( dr["m_parent_mid"]);
				Obj.m_name =  dr["m_name"].ToString();
				Obj.m_url =  dr["m_url"].ToString();
				Obj.m_sortid = (( dr["m_sortid"])==DBNull.Value)?0:Convert.ToInt32( dr["m_sortid"]);
				Obj.m_status = (( dr["m_status"])==DBNull.Value)?0:Convert.ToInt32( dr["m_status"]);
				Obj.m_picname =  dr["m_picname"].ToString();
				Obj.m_level = (( dr["m_level"])==DBNull.Value)?0:Convert.ToInt32( dr["m_level"]);
				Obj.m_remark =  dr["m_remark"].ToString();
				Obj.m_exp1 =  dr["m_exp1"].ToString();
				Obj.m_exp2 = (( dr["m_exp2"])==DBNull.Value)?0:Convert.ToInt32( dr["m_exp2"]);
				Obj.m_add_time = (( dr["m_add_time"])==DBNull.Value)?Convert.ToDateTime(1900-1-1):Convert.ToDateTime( dr["m_add_time"]);
			
			return Obj;
		}
		#endregion
		
		/// <summary>
		/// 根据ID,返回一个Menus对象
		/// </summary>
		/// <param name="m_id">m_id</param>
		/// <returns>Menus对象</returns>
		public MenusEntity Get_MenusEntity (int m_id)
		{
			MenusEntity _obj=null;
			SqlParameter[] _param={
			new SqlParameter("@m_id",SqlDbType.Int)
			};
			_param[0].Value=m_id;
			string sqlStr="select * from Menus where m_id=@m_id";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(Conn.SqlConn,CommandType.Text,sqlStr,_param))
			{
				while(dr.Read())
				{
            		_obj=Populate_MenusEntity_FromDr(dr);
				}
			}
			return _obj;
		}
		/// <summary>
		/// 得到数据表Menus所有记录
		/// </summary>
		/// <returns>数据集</returns>
		public IList< MenusEntity> Get_MenusAll()
		{
			IList< MenusEntity> Obj=new List< MenusEntity>();
			string sqlStr="select * from Menus";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(Conn.SqlConn,CommandType.Text,sqlStr))
			{
				while(dr.Read())
				{
					Obj.Add(Populate_MenusEntity_FromDr(dr));
				}
			}
			return Obj;
		}
		/// <summary>
        /// 检测是否存在根据主键
        /// </summary>
        /// <param name="m_id">m_id</param>
        /// <returns>是/否</returns>
		public bool IsExistMenus(int m_id)
		{
			SqlParameter[] _param={
                                      new SqlParameter("@m_id",SqlDbType.Int)
                                  };
            _param[0].Value=m_id;
            string sqlStr="select Count(*) from Menus where m_id=@m_id";
            int a=Convert.ToInt32(SqlHelper.ExecuteScalar(Conn.SqlConn,CommandType.Text,sqlStr,_param));
            if(a>0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}

        #endregion
		
		#region----------自定义实例化接口函数----------
		#endregion
    }
}
